package com.example.andoridclient;

import com.baidu.mapapi.SDKInitializer;
import com.example.andoridclient.R.id;

import fragment.CreateOrderFragment;
import fragment.LocationFragment;
import fragment.OrderFragment;
import fragment.SettingFragment;
import global.Global;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener {

	Button button_orderlist;
	Button button_settinglist;
	Button button_setorder;
	Button button_location;
	
	
	static Fragment currentfragment;
	static public OrderFragment orderfragment;
	static SettingFragment settingfragment;
	static public CreateOrderFragment creatorderfragment;
	static LocationFragment locationfragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		button_orderlist = (Button) findViewById(R.id.buttonOrderListPage);
		button_settinglist = (Button) findViewById(R.id.buttonSettingListPage);
		button_setorder = (Button) findViewById(id.buttonSetOrderPage);
		button_location = (Button) findViewById(id.buttonLocationPage);
		
		button_orderlist.setOnClickListener(this);
		button_settinglist.setOnClickListener(this);
		button_setorder.setOnClickListener(this);
		button_location.setOnClickListener(this);

		setDefaultFragment();
		
		if (!Global.user.Role.equals("Sendout")) {
			button_setorder.setVisibility(View.GONE);
		}
		
		SDKInitializer.initialize(getApplicationContext()); 
	}

	// 设置默认fragment
	private void setDefaultFragment() {
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		orderfragment = new OrderFragment();
		currentfragment = orderfragment;
		transaction.replace(R.id.id_content, orderfragment);
		transaction.commit();
	}

	// 点击事件
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonOrderListPage:
			if (orderfragment == null) {
				orderfragment = new OrderFragment();
			}
			// 使用当前Fragment的布局替代id_content的控件
			switchContent(orderfragment);
			break;
		case R.id.buttonSettingListPage:
			if (settingfragment == null) {
				settingfragment = new SettingFragment();
			}
			switchContent(settingfragment);
			break;
		case R.id.buttonSetOrderPage:
			if (creatorderfragment == null) {
				creatorderfragment = new CreateOrderFragment();
			}
			switchContent(creatorderfragment);
			break;
		case R.id.buttonLocationPage:
			if (locationfragment == null) {
				locationfragment = new LocationFragment();
			}
			switchContent(locationfragment);
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void switchContent(Fragment next) {
		if (currentfragment != next) {
			FragmentTransaction transaction = getFragmentManager()
					.beginTransaction();
			if (!next.isAdded()) { // 先判断是否被add过
				transaction.hide(currentfragment).add(R.id.id_content, next)
						.commit(); // 隐藏当前的fragment，add下一个到Activity中
			} else {
				transaction.hide(currentfragment).show(next).commit(); // 隐藏当前的fragment，显示下一个
			}
			currentfragment = next;
		}
	}
}
